package com.onstar.cn.vehicle;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.util.XMLUtil;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.framework.network.http.Sender;
import com.onstar.cn.sqlite.entities.PollingEntity;
import com.onstar.cn.vehicle.entity.VehicleContext;
import com.onstar.cn.vehicle.receive.BrocastReceiveFactory;
import com.onstar.cn.vehicle.request.LockDoorsRequest;

public class TestVehActivity extends Activity {
	
	private BroadcastReceiver receiver;
	private PollingEntity entity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		entity = new PollingEntity();
		entity.setRequestStatus("inProgress");
		entity.setRequestType("LockDoorsRequest");
		entity.setVin("LSGAR5AL2G4240007");
//		entity.setSessionKey("5c953343-1cf1-3f83-9302-c2f5872983cb");
		receiver = new BrocastReceiveFactory();
		registerReceiver(receiver, new IntentFilter("nav"));
	}
	
	public void handlerResult(String result) {
		Toast.makeText(this, "show result handler", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}
	
	@Request(heads = @HeadsValue(accept = HttpAccept.XML, language = Language.ZH) , method = HttpMethod.POST)
	public void httpRequest() {
		LockDoorsRequest lockDoorsRequest = new LockDoorsRequest();
		VehicleContext context = new VehicleContext();
//		context.setAppSessionKey(entity.getSessionKey());
//		context.setMeta(VehicleUtil.getMetaData());
		context.setVin(entity.getVin());
//		context.setRoot("GetRequestStatusesRequest");
//		context.setXmlns("http://asd.onstar.com/partnerservices/ps");
		lockDoorsRequest.setContext(context);
		lockDoorsRequest.setDelayTime((short)0);
		String xml = XMLUtil.toXml(lockDoorsRequest);
		
		RequestEntity requestEntity = new RequestEntity();
		requestEntity.setBody(xml);
		requestEntity.setUrl("https://apigee-gw.sit.shanghaionstar.com.cn:2443/PartnerServices/REST/VehicleServices/initiateLockDoors");
		Sender sender = new Sender();
		sender.send(requestEntity, new ResponseHandler() {
			@Override
			public void onSuccess(String tag, String json) {
				if(json.contains("success")) {
//					Intent intent = new Intent(TestVehActivity.this, VehiclePollingService.class);
//					startService(intent);
				}
			}
			@Override
			public void onError(String tag, HttpException e) {
				
			}

		}, getClass());
	}
}
